package com.example.myapplication.SuiPian;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.example.myapplication.Config.OKHttpTool;
import com.example.myapplication.R;
import com.example.myapplication.ShuJu.BingnewlistBean;
import com.example.myapplication.ShuJu.NewsCarouselmapBean;
import com.example.myapplication.ShuJu.NewsclassificationBean;
import com.example.myapplication.apter.BingnewlistApter;
import com.example.myapplication.apter.NewsCarouselmapApter;
import com.google.android.material.tabs.TabLayout;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;


public class NewsFragment extends Fragment {


    private View view;
    private ViewPager news_img;
    private TabLayout news_tab;
    private RecyclerView news_list;
    private OkHttpClient okHttpClient;
    private Request request;
    private NewsCarouselmapBean carouselmapBean;
    private Gson gson;
    private Handler handler;
    private NewsclassificationBean newsclassificationBean;
    private BingnewlistBean bingnewlistBean;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_news, container, false);
        initview();
        //轮播图
        Carouselmap();
        //新闻分类
        Newsclassification();
        bingnewlist(9);
        return view;
    }

    private void Newsclassification() {
        request = new Request.Builder()
                .url("http://124.93.196.45:10001/prod-api/press/category/list")
//                .addHeader("Authorization", OKHttpTool.TOKEN)
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {

            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String json = response.body().string();
                newsclassificationBean = gson.fromJson(json, NewsclassificationBean.class);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        for (NewsclassificationBean.DataBean datum : newsclassificationBean.getData()) {
                            TabLayout.Tab tab = news_tab.newTab();
                            tab.setText(datum.getName());
                            news_tab.addTab(tab);
                        }
                        sendnewlist();
                    }
                });
            }
        });
    }

    private void sendnewlist() {
        news_tab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                String name = tab.getText().toString();
                if (newsclassificationBean.getData().get(position).getName().equals(name))
                    bingnewlist(newsclassificationBean.getData().get(position).getId());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private void bingnewlist(Integer integer) {
        request = new Request.Builder()
                .url("http://124.93.196.45:10001/prod-api/press/press/list?type=" + integer)
//                .addHeader("Authorization",OKHttpTool.TOKEN)
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String json = response.body().string();
                bingnewlistBean = gson.fromJson(json, BingnewlistBean.class);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        BingnewlistApter bingnewlistApter = new BingnewlistApter(NewsFragment.this.getContext(), bingnewlistBean.getRows());
                        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(NewsFragment.this.getContext());
                        news_list.setAdapter(bingnewlistApter);
                        news_list.setLayoutManager(linearLayoutManager);
                    }
                });
            }
        });
    }


    private void Carouselmap() {
        request = new Request.Builder()
                .url("http://124.93.196.45:10001/prod-api/press/press/list")
                /*.addHeader("Authorization", OKHttpTool.TOKEN)*/
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String json = response.body().string();
                carouselmapBean = gson.fromJson(json, NewsCarouselmapBean.class);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        gebean();
                    }
                });
            }
        });
    }

    private void gebean() {
        NewsCarouselmapApter newsCarouselmapApter = new NewsCarouselmapApter(NewsFragment.this.getContext(), carouselmapBean.getRows());
        news_img.setAdapter(newsCarouselmapApter);
        mHandler.sendEmptyMessageDelayed(0, 3 * 1000);
    }

    private void initview() {
        news_img = view.findViewById(R.id.news_img);
        news_tab = view.findViewById(R.id.news_tab);
        news_list = view.findViewById(R.id.news_list);

        gson = new Gson();
        handler = new Handler();
        okHttpClient = new OkHttpClient();

    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            if (msg.what == 0) {
                int index = news_img.getCurrentItem();
                index = (index + 1) % 4;
                news_img.setCurrentItem(index);
                mHandler.sendEmptyMessageDelayed(0, 3 * 1000);
            }
        }
    };


}